package com.ant.androidBuddy;

import java.io.*;
import java.util.*;



import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.*;
import android.widget.AdapterView.OnItemClickListener;

public class OpenFileActivity extends Activity{

	private ListView mainListView ;  
	private ArrayAdapter<String> listAdapter ;  

	/** Called when the activity is first created. */  
	@Override  
	public void onCreate(Bundle savedInstanceState) {  
		super.onCreate(savedInstanceState);  
		setContentView(R.layout.open_file);  

		ActionBar actionBar=getActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);

		// Find the ListView resource.   
		mainListView = (ListView) findViewById( R.id.mainListView );  


		// Create and populate a List of planet names.  
		String[] savedFiles=fileList();

		ArrayList<String> myList = new ArrayList<String>();  
		myList.addAll( Arrays.asList(savedFiles) );  

		// Create ArrayAdapter using the planet list.  
		listAdapter = new ArrayAdapter<String>(this, R.layout.simple_row, myList);   

		// Set the ArrayAdapter as the ListView's adapter.  
		mainListView.setAdapter( listAdapter );   

		mainListView.setOnItemClickListener(new OnItemClickListener() {


			public void onItemClick(AdapterView<?> arg0, View view, int pos,
					long id) {

				// String name=view.findViewById(R.id.rowTextView);
				TextView text = (TextView) view;
				String name = (String) text.getText();
				String content="";
				// try opening the myfilename.txt
				try {
					// open the file for reading
					InputStream instream = openFileInput(name);

					// if file the available for reading
					if (instream!=null) {
						// prepare the file for reading
						InputStreamReader inputreader = new InputStreamReader(instream);
						BufferedReader buffreader = new BufferedReader(inputreader);

						String line="";

						// read every line of the file into the line-variable, on line at the time
						while (( line = buffreader.readLine()) != null) {
							content+=line+"\n";
						}
						//remove final new line
						content=content.substring(0, content.length()-1);

					}

					// close the file again       
					instream.close();
				} catch (java.io.FileNotFoundException e) {
					// do something if the myfilename.txt does not exits
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				Intent intent = new Intent(OpenFileActivity.this, AnimationActivity.class); 
				intent.putExtra("savedtxt", content);
				startActivity(intent);				
			}
		});
	}  
	public boolean onOptionsItemSelected(MenuItem item) 
	{    
		switch (item.getItemId()) 
		{        
		case android.R.id.home:{
			Intent intent = new Intent(this, AndroidBuddyActivity.class);            
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);            
			return true;
		}
		default:   {  
			return super.onOptionsItemSelected(item); 
		}
		}
	}
}
